package top.jle.jpa.basic.dao;
import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;
/**
 * 分页查询基本参数封装类.
 * 
 * @author Senon Ye
 *
 */
public class PageRequest implements Serializable{
	
	/**
	 * 
	 */
	@Serial
	private static final long serialVersionUID = -6852716105693156038L;

	private int offset = 0;
	
	private int  limit = 0;

	private transient SortKey[] orderBy = null;
	
	private boolean countTotal = true;//是否计算总记录数.
	
	private int currentPage; // 当前页
	
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public PageRequest() {
	}	

	/**
	 * @param offset 跳过行数(从0开始)
	 * @param limit 限制查询的条数
	 * @param orderBy 不传,传null或new SortKey[]{}空数组,均默认id降序处理
	 */
	public  PageRequest(int offset,int limit,SortKey ... orderBy) {
		this.setOffset(offset);
		this.setLimit(limit);	
		this.orderBy = orderBy;	
	}	
	
	public int getOffset() {
		return this.offset;
	}

	/**
	 * 设置跳过行数(从0开始),低于0时自动调整为0.
	 */
	public void setOffset(final int offset) {
        this.offset = Math.max(offset, 0);
	}	

	/**
	 * 获得查询限制的记录数量
	 */
	public int getLimit() {
		return this.limit;
	}
	
	/**
	 * 设置限制查询的记录数量
	 */
	public void setLimit(final int limit) {	
		this.limit = limit;		
	}


	public SortKey[] getOrderBy() {
		if(this.orderBy != null){
			return Arrays.copyOf(this.orderBy, this.orderBy.length);
		}else{
			return new SortKey[]{};
		}
	}

	public void setOrderBy(final SortKey[] orderBy) {
		if(orderBy != null){
			this.orderBy = Arrays.copyOf(orderBy, orderBy.length);
		}else{
			this.orderBy = null;
		}
	}
	
	/**
	 * 是否默认计算总记录数.
	 */
	public boolean isCountTotal() {
		return countTotal;
	}

	/**
	 * 设置是否默认计算总记录数.
	 */
	public void setCountTotal(boolean countTotal) {
		this.countTotal = countTotal;
	}
	
}
